package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientInfoMapper;
import com.leyou.auth.service.ClientsAuthService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientsAuthServiceImpl extends ServiceImpl<ClientInfoMapper, ClientInfo> implements ClientsAuthService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;

    //微服务申请秘钥效验
    @Override
    public String getSecretKey(String clientId, String secret) {

        ClientInfo client = this.query().eq(true, "client_id", clientId).one();
        if (client == null) {
            throw new LyException("此为服务没有权限", 400);
        }

        if (!passwordEncoder.matches(secret,client.getSecret())) {
            throw new LyException("微服务密码错误", 400);
        }

        System.out.println("微服务申请秘钥效验成功");
        return key;

    }
}
